﻿using AutoMapper;
using B.S.Procurement.Domain;
using B.S.Procurement.Error;
using B.S.Procurement.Read.Api.Application.Command.PurchaseDetailsCommand;
using B.S.Procurement.Read.Api.DTO;
using MediatR;
using SqlSugar;

namespace B.S.Procurement.Read.Api.Application.CommandHander.PurchaseDetailsCommandHander
{
    public class QueryPurchaseDetailsCommandHander : IRequestHandler<QueryPurchaseDetailsCommand, ApiPaging<List<PurchaseDto>>>
    {
        private readonly IMapper mapper;
        private readonly ISqlSugarClient db;

        public QueryPurchaseDetailsCommandHander(IMapper mapper, ISqlSugarClient db)
        {
            this.mapper = mapper;
            this.db = db;
        }

        public Task<ApiPaging<List<PurchaseDto>>> Handle(QueryPurchaseDetailsCommand request, CancellationToken cancellationToken)
        {
            ApiPaging<List<PurchaseDto>> result = new ApiPaging<List<PurchaseDto>>();

            var item = db.Queryable<PurchaseDetails>()
                       .InnerJoin<ProcurementModel>((a, b) => a.ProcurementId == b.ProcurementId)
                       .InnerJoin<VendorModel>((a, b, c) => a.VendorId == c.VendorId)
                       .Where((a, b, c) => a.Del == false)
                       .Select((a, b, c) => new PurchaseDto
                       {
                           DetailsId = a.DetailsId,
                           ProductName = a.ProductName,
                           ProductNumber = a.ProductNumber,
                           Specification = a.Specification,
                           VendorId = c.VendorId,
                           Unit = a.Unit,
                           Number = a.Number,
                           Price = a.Price,
                           SumMoney = a.SumMoney,
                           DateRequired = a.DateRequired,
                           Remark = a.Remark,
                           Del = a.Del,
                           ProcurementId = b.ProcurementId,

                           VendorName = c.VendorName,
                           SupplierNumber = c.SupplierNumber,
                           VendorTypeId = c.VendorTypeId,
                           Industry = c.Industry,
                           Contact = c.Contact,
                           Sex = c.Sex,
                           Origin = c.Origin,
                           Phone = c.Phone,
                           Email = c.Email,
                           Addr = c.Addr,
                           CompanyProfile = c.CompanyProfile,
                           Bank = c.Bank,
                           AccountName = c.AccountName,
                           BankCardNumber = c.BankCardNumber,
                       });

            result.TotalCount = item.Count();
            result.TotalPage = (int)Math.Ceiling(result.TotalCount * 1.0 / request.PageSize);
            item = item.OrderByDescending(a => a.ProcurementId).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize);

            result.Code = ApiEnum.success;
            result.Msg = "查询成功";
            result.Data = mapper.Map<List<PurchaseDto>>(item.ToList());

            return Task.FromResult(result);
        }
    }
}
